package com.example.hdwx.myapplication.HomeActivity.cooklist_fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.hdwx.myapplication.R;
import com.example.hdwx.myapplication.adapter.CookList_adapter;
import com.example.hdwx.myapplication.eneity.Cooklist_enetity;
import com.example.hdwx.myapplication.untils.HttpUntils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/2/12.
 */

public class Cooklist_Fragment extends Fragment {
    public RecyclerView recyclerView;
    public List<Cooklist_enetity> mdata = new ArrayList<>();
    public CookList_adapter adapter;
    private Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取布局查找控件4
        View view = inflater.inflate(R.layout.cooklist_fragment, container, false);
        recyclerView= (RecyclerView) view.findViewById(R.id.cooklist_recycleview);
        //设置布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        //设置方向（默认垂直vetical)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        load();
    }

    //把okhttp下载框架封装到Load方法中

    //    public void load(){
//        //固定格式
//        OkHttpUtils.get()
//                .url(new Api().PATH(0))
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    //下载失败调用的方法
//                    public void onError(Request request, Exception e) {
//                        Log.e("erro","下载失败");
//                    }
//                    //下载成功调用的方法
//                    @Override
//                    public void onResponse(String response) {
//                        //解析json数据，得到一个List<Daydaycook_cooklist>的集合；
//                        mdata=Cooklist_Json.cooklist_json(response);
//                        //获取adapter
//                        //adapter = new CookList_adapter(getContext(),mdata);
//                        //绑定适配器
//                        //recyclerView.setAdapter(adapter);
//                    }
//                });
//    }
    public void load(){
        adapter = new CookList_adapter(getContext(),mdata);
        adapter.setLoadCallback(new CookList_adapter.ILoadCallback() {
            @Override
            public void onLoad() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<Cooklist_enetity> list = new ArrayList<>();
                        for (int i = mdata.size(); i < mdata.size()+10; i++) {
                            //获取数据
                            list = HttpUntils.gethttp();
                        }
                        adapter.addData(list);
                        mdata.addAll(list);
                    }
                },2000);
            }
        });
        recyclerView.setAdapter(adapter);
    }
}
